package setOfEquation;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SetOfEquationGUI extends JFrame implements ActionListener, KeyListener{

	JLabel lblA, lblB, lblC,lblD,lblE,lblF, lblRoof, lblTitle;
	JTextField txtA, txtB, txtC,txtD, txtE, txtF;
	JTextArea txtRoof;
	JButton btnSolve, btnReset, btnExit;
	Font fontBold = new Font(" Times New Roman",Font.BOLD,16 );
	Font fontItalic = new Font(" Times New Roman",Font.ITALIC,12 );
	JPanel jplA, jplB, jplC,jplD, jplE, jplF, jplRoof, jplButton;

	public SetOfEquationGUI() 
	{
		lblA = new JLabel("ENTER A:   ");
		lblB = new JLabel("ENTER B:   ");
		lblC = new JLabel("ENTER C:   ");
		lblD = new JLabel("ENTER D:   ");
		lblE = new JLabel("ENTER E:   ");
		lblF = new JLabel("ENTER F:   ");
		lblRoof = new JLabel("Result:    ");
		lblTitle = new JLabel("       SET OF EQUATION SOLVE PROGRAM     ");
		lblTitle.setFont(fontBold);

		txtA = new JTextField(8);
		txtB = new JTextField(8);
		txtB.addActionListener(this);
		txtC = new JTextField(8);
		txtC.addActionListener(this);
		txtD = new JTextField(8);
		txtD.addActionListener(this);
		txtE = new JTextField(8);
		txtE.addActionListener(this);
		txtF = new JTextField(8);
		txtF.addActionListener(this);
		txtRoof = new JTextArea(8,0);
		txtRoof.setEditable(false);
		txtRoof.setBackground(new Color(50,200,100));
		txtA.addKeyListener(this);
		txtB.addKeyListener(this);
		txtC.addKeyListener(this);
		txtD.addKeyListener(this);
		txtE.addKeyListener(this);
		txtF.addKeyListener(this);

		btnSolve = new JButton("Solve");
		btnSolve.addActionListener(this);
		btnSolve.addKeyListener(this);
		btnReset = new JButton("Reset");
		btnReset.addActionListener(this);
		btnReset.addKeyListener(this);
		btnExit = new JButton("Exit");
		btnExit.addActionListener(this);
		btnExit.addKeyListener(this);

		jplA= new JPanel();
		jplA.setLayout(new GridLayout(1,2,5,5));
		jplA.add(lblA);
		jplA.add(txtA);

		jplB= new JPanel();
		jplB.setLayout(new GridLayout(1,2,5,5));
		jplB.add(lblB);
		jplB.add(txtB);

		jplC= new JPanel();
		jplC.setLayout(new GridLayout(1,2,5,5));
		jplC.add(lblC);
		jplC.add(txtC);
		
		jplD= new JPanel();
		jplD.setLayout(new GridLayout(1,2,5,5));
		jplD.add(lblD);
		jplD.add(txtD);

		jplE= new JPanel();
		jplE.setLayout(new GridLayout(1,2,5,5));
		jplE.add(lblE);
		jplE.add(txtE);
		
		jplF= new JPanel();
		jplF.setLayout(new GridLayout(1,2,5,5));
		jplF.add(lblF);
		jplF.add(txtF);
		
		jplRoof = new JPanel();
		jplRoof.setLayout(new GridLayout(1,2,5,5));
		jplRoof.add(lblRoof);
		jplRoof.add(txtRoof);

		jplButton = new JPanel();
		jplButton.setLayout(new GridLayout(1,3,5,5));
		jplButton.add(btnSolve);
		jplButton.add(btnReset);
		jplButton.add(btnExit);

		setLayout(new GridLayout(9,1,10,10));
		
		add(lblTitle);
		add(jplA);
		add(jplB);
		add(jplC);
		add(jplD);
		add(jplE);
		add(jplF);
		add(jplRoof);
		add(jplButton);

	}
	public String solve(){
		Solve s=new Solve();
		s.setA(Integer.parseInt(txtA.getText()));
		s.setB(Integer.parseInt(txtB.getText()));
		s.setC(Integer.parseInt(txtC.getText()));
		s.setD(Integer.parseInt(txtD.getText()));
		s.setE(Integer.parseInt(txtE.getText()));
		s.setF(Integer.parseInt(txtF.getText()));
		return s.solve();
	}
	public void actionPerformed(ActionEvent e){
		if (e.getSource()== btnExit)
			System.exit(0);
		else
			if (e.getSource()==btnReset) {
				txtA.setText("");
				txtB.setText("");
				txtC.setText("");
				txtD.setText("");
				txtE.setText("");
				txtF.setText("");
				txtRoof.setText("");
				txtA.requestFocus();
			}
			else 
				if(e.getSource()==btnSolve){
					try{
						txtRoof.setText(solve());
						btnReset.requestFocus();
					}catch(Exception ex){	
						JOptionPane.showMessageDialog(null, "You must input the value number");
						txtA.setText("");
						txtB.setText("");
						txtC.setText("");
						txtD.setText("");
						txtE.setText("");
						txtF.setText("");
						txtA.requestFocus();
					}	
				}
	}
	public void keyPressed(KeyEvent e){
		if (e.getSource()==btnReset){
			if(e.getKeyCode()==37) btnSolve.requestFocus();
			if(e.getKeyCode()==39) btnExit.requestFocus();
			if(e.getKeyCode()==KeyEvent.VK_ENTER){
				txtA.setText("");
				txtB.setText("");
				txtC.setText("");
				txtD.setText("");
				txtE.setText("");
				txtF.setText("");
				txtRoof.setText("");
				txtA.requestFocus();
			}
		}
		else 
		if (e.getSource()== btnExit){
			if(e.getKeyCode()==37) btnReset.requestFocus();			
			if(e.getKeyCode()== 10)
				System.exit(0);
		}
		else
		if(e.getSource()==btnSolve){
			if(e.getKeyCode()==38||e.getKeyCode()==37) txtA.requestFocus();
			if(e.getKeyCode()==39) btnReset.requestFocus();
			if(e.getKeyCode()==10){
				try{
				txtRoof.setText(solve());
				btnReset.requestFocus();
				}catch(Exception ex){	
					JOptionPane.showMessageDialog(null, "You must input the value number");
					txtA.setText("");
					txtB.setText("");
					txtC.setText("");
					txtD.setText("");
					txtE.setText("");
					txtF.setText("");
					txtA.requestFocus();
				}	
			}
		}
		else
		if(e.getSource()==txtA){
			if(e.getKeyCode()==10||e.getKeyCode()==40)
				try{
					Integer.parseInt(txtA.getText());
					txtB.requestFocus();
				}catch(Exception ex){	
					JOptionPane.showMessageDialog(null, "You must input the value number");
					txtA.setText("");	
				}
		}
		else 
		if(e.getSource()==txtB){
			if(e.getKeyCode()==38) txtA.requestFocus();
			if(e.getKeyCode()==10||e.getKeyCode()==40)
				try{
					Integer.parseInt(txtB.getText());
					txtC.requestFocus();
				}catch(Exception ex){	
					JOptionPane.showMessageDialog(null, "You must input the value number");
					txtB.setText("");
				}	
		}
		else
		if(e.getSource()==txtC){
			if(e.getKeyCode()==38) txtB.requestFocus();
			if(e.getKeyCode()==10||e.getKeyCode()==40)
				try{
					Integer.parseInt(txtC.getText());
					txtD.requestFocus();
				}catch(Exception ex){	
					JOptionPane.showMessageDialog(null, "you must input the value number");
					txtC.setText("");
				}
		}
		else
		if(e.getSource()==txtD){
			if(e.getKeyCode()==38) txtD.requestFocus();
			if(e.getKeyCode()==10||e.getKeyCode()==40)
				try{
						Integer.parseInt(txtD.getText());
						txtE.requestFocus();
					}catch(Exception ex){	
						JOptionPane.showMessageDialog(null, "You must input the value number");
						txtD.setText("");
					}
			}
		else
			if(e.getSource()==txtE){
				if(e.getKeyCode()==38) txtE.requestFocus();
				if(e.getKeyCode()==10||e.getKeyCode()==40)
					try{
						Integer.parseInt(txtE.getText());
						txtF.requestFocus();
					}catch(Exception ex){	
						JOptionPane.showMessageDialog(null, "You must input the value number");
						txtE.setText("");
					}
			}
			else
				if(e.getSource()==txtF){
					if(e.getKeyCode()==38) txtF.requestFocus();
					if(e.getKeyCode()==10||e.getKeyCode()==40)
						try{
							Integer.parseInt(txtF.getText());
							btnSolve.requestFocus();
						}catch(Exception ex){	
							JOptionPane.showMessageDialog(null, "You must input the value number");
							txtF.setText("");
						}
		}
	}
	public void keyTyped(KeyEvent e){}
	public void keyReleased(KeyEvent e){
	}
	public static void main(String[] args) {
		
		SetOfEquationGUI frame = new SetOfEquationGUI();
		frame.setTitle("Square Equation GUI");
		frame.setLocationRelativeTo(null); // Center the frame
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 400);
		frame.setVisible(true);

	}

}
